package cn.tedu.stringx;


public class StringDemo {
    public static void main(String[] args) {
        //concat拼接
//        String str = "hello";
//        String res = str.concat("world");//创建新的数组对象  创建String对象
//        System.out.println(res);
        //+ 字符串拼接 编译时进行优化
        //String str = new StringBuilder().append("hello").append("world").toString();
        //创建了StringBuilder对象 toString时创建了String对象
//        String str = "hello"+"world";
//        System.out.println(str);
//        new StringBuilder().append("a").toString();
//        new StringBuffer();

        //创建StringBuilder对象
//        StringBuilder builder = new StringBuilder();//不包含任何内容
//        System.out.println(builder);//重写了toString方法
//        StringBuilder builder = new StringBuilder("hello");//不包含任何内容
//        System.out.println(builder);

        //循环拼接
        String str = "start";
        //一次循环创建两个对象 new StringBuilder()  new String
        //20000个对象
        for (int i = 0; i <10000 ; i++) {
            str = str+i+",";//new StringBuilder().append(str).append(i).append(",").toString()
        }

        //StringBuidler
        //new了两个对象
        StringBuilder builder = new StringBuilder("start");//new StringBuilder
        for (int i = 0; i <10000 ; i++) {
            builder.append(i).append(",");
        }
        String res = builder.toString();//new String()
        System.out.println(res);
    }
}
